package io;

import java.io.*;
import java.nio.charset.StandardCharsets;

/*缓冲输出流的缓冲区问题*/
public class BOSDemo {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("src/io/bos.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        String line = "123";
        byte[] data = line.getBytes(StandardCharsets.UTF_8);
        /*data内容不够8k,依旧还在缓冲区*/
        bos.write(data);
        /*void flush():将缓冲区数据一次性写出,缓冲流的close()会自动调用flush()
        * flush()写出即时性会更好*/
        bos.flush();
        bos.close();
        System.out.println("写出完毕");
        /*输出流close()有无*/
        File file = new File("src/io/bos.txt");
        FileInputStream fis  = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        byte[] data1 = new byte[(int)file.length()];
        String line1 = new String(data,StandardCharsets.UTF_8);
        System.out.println(line1);
        bis.close();
    }
}
